home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ov143b.zip / OVTTY.C < prev    next >
C/C++ Source or Header  |  1993-01-04  |  13KB  |  359 lines

  1. /*  051  31-May-87  ovtty.c
  2.  
  3.         Copyright (c) 1987 by Blue Sky Software.  All rights reserved.
  4. */
  5.  
  6. #include <dos.h>
  7. #include "ov.h"
  8. #include "overr.h"
  9. #include "direct.h"
  10.  
  11. static int vmode, vwidth, vpage;          /* initial video mode, width, page */
  12. static int crow, ccol, cstscan, cendscan; /* initial cursor stuff */
  13. static int save_row, save_col, save_stscan, save_endscan; /* save cursor */
  14.  
  15. char far *screen = NULL;               /* address of screen image */
  16. char far *cursor = NULL;               /* cursor location on screen */
  17. static char far *save_cursor;               /* loc to save cursor value */
  18. static int imageidx = 0;                    /* index of next image[] to use */
  19. static char far *image[2] = { NULL, NULL }; /* address of saved screen image */
  20. static char far *savcursor[2];              /* saved cursor location */
  21. char far *malloc_f();
  22.  
  23. unsigned char vid_attrib;              /* current video attribute to use */
  24. unsigned char vid_mode;                /* video mode in use */
  25. unsigned char attribs[7];              /* video attrib's in use */
  26.  
  27. extern unsigned char vid_snow;         /* NZ if must chk for video snow */
  28. extern unsigned char monattr[];        /* video attributes for mono display */
  29. extern unsigned char cgaattr[];        /* video attributes for cga  display */
  30.  
  31. /******************************************************************************
  32.  **                 I N I T  /  R E S E T _ T T Y                            **
  33.  *****************************************************************************/
  34.  
  35. void
  36. init_tty() {           /* initialize the display (terminal?) */
  37.  
  38.    vmode = getvideomode(&vwidth,&vpage);   /* save current video state */
  39.  
  40.    /* initialize the display adapter, currently only modes 3 and 7
  41.       are supported.  Set the mode (which also clears the screen),
  42.       and if a CGA, make sure display page 0 is active */
  43.  
  44.    if (vmode < 7) {                            /* initialize a CGA */
  45.       setvideomode(vid_mode = 3);              /* color, 80x25 text */
  46.       setdisplaypage(0);                       /* display page 0 */
  47.       strncpy(attribs,cgaattr,sizeof(attribs));/* set up cga display attr's */
  48.  
  49.       FP_SEG(screen) = 0xB800;                 /* set screen base address */
  50.  
  51.    } else {                                    /* initialize a MONO adapter */
  52.       setvideomode(vid_mode = 7);              /* clears the screen */
  53.       strncpy(attribs,monattr,sizeof(attribs));/* set up mono display attr's */
  54.       vid_snow = 0;                            /* no snow on mono */
  55.       FP_SEG(screen) = 0xB000;                 /* set screen base address */
  56.    }
  57.  
  58.    setvattrib(DIS_NORM);          /* set default video attribute */
  59.    cursor = screen;               /* cursor is at top of screen */
  60.  
  61.    /* save data about the cursor position and size, we're going
  62.       to turn off the cursor and we want to be able to restore
  63.       it later */
  64.  
  65.    readcursor(vpage,&crow,&ccol,&cstscan,&cendscan);  /* save info */
  66.  
  67.    setcursorsize(32,0);           /* turn off hardware cursor */
  68. }
  69.  
  70. void
  71. reinit_tty() {         /* reinitialize tty from unknown state */
  72.  
  73.    int mode, dummy;
  74.  
  75.    mode = getvideomode(&dummy,&dummy); /* get current video mode */
  76.    if (mode != vid_mode)               /* reset video mode if need be */
  77.       setvideomode(vid_mode);
  78.    if (vid_mode == 3)                  /* set display page 0 if cga */
  79.       setdisplaypage(0);
  80.    hidecursor();                       /* make sure cursor is off */
  81. }
  82.  
  83. void
  84. reset_tty() {         /* restore the display to pre-overview status */
  85.  
  86.    setvideomode(vmode);                   /* restore the video mode */
  87.  
  88.    setdisplaypage(vpage);                 /* restore display page */
  89.  
  90.    setcursorsize(cstscan,cendscan);       /* turn the curosr back on */
  91. }
  92.  
  93.  
  94. /******************************************************************************
  95.  **                      S E T V A T T R I B                                 **
  96.  *****************************************************************************/
  97.  
  98. void ALTCALL
  99. setvattrib(i)          /* set the video attribute */
  100. int i;
  101. {
  102.    vid_attrib = attribs[i];
  103. }
  104.  
  105.  
  106. /******************************************************************************
  107.  **                       D I S P _ S T R _ A T                              **
  108.  *****************************************************************************/
  109.  
  110. void ALTCALL
  111. disp_str_at(s,r,c)     /* display a string at specified row, column */
  112. char *s;
  113. int r,c;
  114. {
  115.    gotorc(r,c);        /* goto display location */
  116.    disp_str(s);        /* use our own routine */
  117. }
  118.  
  119.  
  120. /******************************************************************************
  121.  **                    D I S P _ C H A R _ A T                               **
  122.  *****************************************************************************/
  123.  
  124. void ALTCALL
  125. disp_char_at(ch,r,c)   /* display a char at specified row, column */
  126. int ch, r, c;
  127. {
  128.    gotorc(r,c);        /* goto display location */
  129.    disp_char(ch);      /* use our other routine to display it */
  130. }
  131.  
  132.  
  133. /******************************************************************************
  134.                          D I S P _ R E P _ A T
  135.  *****************************************************************************/
  136.  
  137. void ALTCALL
  138. disp_rep_at(ch,cnt,r,c)        /* display cnt ch's at specified row, column */
  139. int ch, cnt, r, c;
  140. {
  141.    gotorc(r,c);        /* goto display location */
  142.    disp_rep(ch,cnt);   /* use our other routine to display it */
  143. }
  144.  
  145.  
  146. /******************************************************************************
  147.                                P U T S T R
  148.  *****************************************************************************/
  149.  
  150. putstr(s)                      /* display string while updating hardware */
  151. register char *s;              /*   cursor (via putchr() */
  152. {
  153.    while (*s)
  154.       putchr(*s++);
  155. }
  156.  
  157. putstr_nomove(s)               /* here's a strange one, it displays a string */
  158. char *s;                       /*   without moving the hardware or software */
  159. {                              /*   cursor */
  160.    int row, col, x;
  161.    char far *savec;
  162.  
  163.    savec = cursor;                     /* save software cursor location */
  164.    readcursor(0,&row,&col,&x,&x);      /* get hardware cursor location */
  165.  
  166.    /* move software cursor to hardware location and display string */
  167.  
  168.    cursor = screen + (row * 160) + (col << 1);
  169.    disp_str(s);                               /* doesn't move hardware cursor */
  170.  
  171.    cursor = savec;             /* restore software cursor */
  172. }
  173.  
  174.  
  175. /******************************************************************************
  176.  **                          G E T C H R                                     **
  177.  *****************************************************************************/
  178.  
  179. getchr() {             /* get a character from the terminal */
  180.  
  181.    register int ch;
  182.  
  183.    static unsigned char kbdmap[] = {
  184.       /* 3B */     HELP, TAG, GOPAR, GOSUB, NEXTT, PREVT,
  185.       /* 41 */     OPENW, CLOSEW, NEXTW, PREVW, 0, 0,
  186.       /* 47 */     HOME, UP, PGUP, 0, LEFT, 0, RIGHT, 0, END,
  187.       /* 50 */     DOWN, PGDN, INS, DEL };
  188.  
  189.    /* get a char from the user with no echo.  If its a normal (not
  190.       extended char, return it as is */
  191.  
  192. readch:
  193.  
  194.    if (ch = getraw())          /* get a char from user with no echo */
  195.       return(ch);              /* return it if it's a normal character */
  196.  
  197.    /*  must be an extended char, map it to an internal code if used */
  198.  
  199.    ch = getraw();              /* get scan code of char */
  200.  
  201.    if (ch >= 0x3b && ch <= 0x53) {     /* one of our mapped codes? */
  202.       ch = kbdmap[ch-0x3b];            /* yes, translate it */
  203.       if (ch == HELP) {                /* is this a cry for help? */
  204.          help();                       /* do the help subsystem */
  205.          goto readch;                  /* then read another char from user */
  206.       } else
  207.          return(ch);                   /* not help, return translated value */
  208.    }
  209.  
  210.    if (ch >= 0x68 && ch <= 0x68 + NUM_UDK)     /* is it a UDK? */
  211.       return(ch - 0x68 + UDK_START);           /* yes, translate it */
  212.  
  213.    goto readch;                /* unused extended char, ignore it, get more */
  214. }
  215.  
  216.  
  217. /******************************************************************************
  218.  **                          O U T _ X                                       **
  219.  *****************************************************************************/
  220.  
  221. void ALTCALL
  222. out_int(num,fsize,fillch)      /* display an int in a fixed length field */
  223. int num, fsize, fillch;
  224. {
  225.    int flen;
  226.    char numstr[11];
  227.  
  228.    itoa(num,numstr,10);
  229.    if ((flen = fsize - strlen(numstr)) > 0)
  230.       disp_rep(fillch,flen);
  231.    disp_str(numstr);
  232. }
  233.  
  234. void ALTCALL
  235. out_long(num,fsize,fillch)     /* display a long in a fixed length field */
  236. long num;
  237. int fsize, fillch;
  238. {
  239.    int flen;
  240.    char numstr[11];
  241.  
  242.    ultoa(num,numstr,10);
  243.    if ((flen = fsize - strlen(numstr)) > 0)
  244.       disp_rep(fillch,flen);
  245.    disp_str(numstr);
  246. }
  247.  
  248. void ALTCALL
  249. out_str(s,fsize,fillch)        /* disp a string (left justified) in a field */
  250. register char *s;
  251. int fsize, fillch;
  252. {
  253.    register char *ep;
  254.    int len, flen, savch;
  255.  
  256.    if ((len = strlen(s)) > fsize) {    /* string too big for field? */
  257.       savch = *(ep = s + fsize);       /* yes, fake it out by temp */
  258.       *ep = '\0';                      /*   setting it to length of fld */
  259.       disp_str(s);                     /*   display it like normal */
  260.       *ep = savch;                     /*   restore it */
  261.  
  262.    } else {                    /* strlen <= field size */
  263.  
  264.       disp_str(s);
  265.       if ((flen = fsize - len) > 0)
  266.          disp_rep(fillch,flen);
  267.    }
  268. }
  269.  
  270.  
  271. /******************************************************************************
  272.  **            L O W   L E V E L   V I D E O   C O N T R O L                 **
  273.  *****************************************************************************/
  274.  
  275. savescreen() {         /* save the current screen image */
  276.  
  277.    /* allocate a far buffer for the screen image */
  278.  
  279.    if ((image[imageidx] = malloc_f(SCREEN_ROWS*SCREEN_COLS*2)) == NULL)
  280.       show_error(0,NO_MEM,1,"No memory to save screen image");
  281.  
  282.    scrcpy(image[imageidx],screen);             /* copy the screen image */
  283.    savcursor[imageidx] = cursor;               /* save the current cursor loc */
  284.    imageidx++;                                 /* "stack" the screen image */
  285. }
  286.  
  287. restorescreen() {      /* restore the saved screen image */
  288.  
  289.    imageidx--;                                 /* "pop off" a screen image */
  290.    scrcpy(screen,image[imageidx]);             /* restore the screen image */
  291.    cursor = savcursor[imageidx];               /* restore the cursor position */
  292.    free_f(image[imageidx]);                    /* release screen memory */
  293. }
  294.  
  295.  
  296. savecursor() {         /* save the current cursor state */
  297.  
  298.    save_cursor = cursor;               /* save the software cursor */
  299.  
  300.    /* save the hardware cursor status */
  301.    readcursor(0,&save_row,&save_col,&save_stscan,&save_endscan);
  302. }
  303.  
  304. restorecursor() {      /* restore cursor to saved status */
  305.  
  306.    union REGS r;
  307.  
  308.    cursor = save_cursor;               /* restore software cursor */
  309.  
  310.    /* restore hardware cursor */
  311.  
  312.    r.h.ah = 2;                 /* position cursor */
  313.    r.h.dh = save_row;
  314.    r.h.dl = save_col;
  315.    r.h.bh = 0;
  316.    int86(16,&r,&r);
  317.  
  318.    setcursorsize(save_stscan,save_endscan);    /* restore cursor size */
  319. }
  320.  
  321.  
  322. hidecursor() {         /* hide the terminal cursor */
  323.    setcursorsize(32,0);           /* turn off hardware cursor */
  324. }
  325.  
  326.  
  327. showcursor() {         /* make the terminal cursor visible */
  328.  
  329.   union REGS rg;
  330.  
  331.   rg.h.ah = 2;                       /* if direct screen i/o is being done, */
  332.   rg.h.dh = FP_OFF(cursor) / 160;    /* the hardware cursor position is not */
  333.   rg.h.dl = (FP_OFF(cursor)%160)>>1; /* updated along with the faked cursor */
  334.   rg.h.bh = 0;                       /* position, sync the hardware to the  */
  335.   int86(16, &rg, &rg);               /* simulated cursor before displaying it */
  336.  
  337.   setcursorsize(cstscan,cendscan);       /* turn the curosr back on */
  338. }
  339.  
  340. void
  341. clr_scr() {            /* clear the screen */
  342.  
  343.    gotorc(0,0);
  344.    disp_rep(' ',SCREEN_ROWS*SCREEN_COLS);
  345.    gotorc(0,0);
  346. }
  347.  
  348. void ALTCALL
  349. clr_eol() {            /* clear to the end of the current line */
  350.  
  351.    char far *cp;
  352.  
  353.    cp = cursor;                        /* start clearing at cursor loc */
  354.  
  355.    disp_rep(' ',(160 - (FP_OFF(cursor) % 160)) >> 1);
  356.  
  357.    cursor = cp;                        /* resotre cursor location */
  358. }
  359.